<template>
    <div class="tree-box">
        <TreeNode v-for="(item,index) in cloneData" :key="index" :data="item" :show-checkbox="showCheckbox"></TreeNode>
    </div>
</template>
<script>
import deepCopy from '../../assets/js/deepCopy.js'
import TreeNode from './node.vue'
export default {
  name: 'dj-tree',
  components: { TreeNode },
  props: {
    data: {
      type: Array,
      default () {
        return []
      }
    },
    showCheckbox: {
      type: Boolean,
      default: false
    }
  },
  created () {
    this.rebuildData()
  },
  data () {
    return {
      cloneData: []
    }
  },
  watch: {
    data () {
      this.rebuildData()
    }
  },
  methods: {
    emitEvent (eventName, data) {
      this.$emit(eventName, data, this.cloneData)
    },
    // 深拷贝源数据，防止修改源数据
    rebuildData () {
      this.cloneData = deepCopy(this.data)
    }
  }
}
</script>
